
# Load our assumptions, Sigma matrix and all that we calculated on script 11.0.

source("11.0.Exchange rates - assumptions and covariance estimation.R")

####################### Minimum Variance Portfolio #####################

ones.row <- matrix(c(1,1,1), nrow = 1) 
ones.row
ones.col <- matrix(c(1,1,1), ncol = 1)
ones.col
Sigma.inv <- solve(Sigma)
Sigma.inv

# Apply the min var portfolio formula. Recall that %*% means "matrix multiplication".
# First attempt: error
solve(ones.row %*% Sigma.inv %*% ones.col) %*% (Sigma.inv %*% ones.col)

# Second attempt: error again
(ones.row %*% Sigma.inv %*% ones.col)^(-1) * (Sigma.inv %*% ones.col)

# Third attempt: success!
w_min <- as.numeric(ones.row %*% Sigma.inv %*% ones.col)^(-1) * (Sigma.inv %*% ones.col)
w_min

# Expected asset return (3,10%)
exp.return.min <- mu.row %*% w_min
exp.return.min

# Variance and standard deviation of asset return
var.returns.min <- t(w_min) %*% Sigma %*% w_min
var.returns.min

sd.returns.min <- sqrt(var.returns.min)
sd.returns.min

####################### Reference Risky Portfolio ####################

# Expected returns on each asset (assumption)
mu.row

# As column vector
mu.col

# Reference risky portfolio
w_ref <- as.numeric(ones.row %*% Sigma.inv %*% mu.col)^(-1) * (Sigma.inv %*% mu.col)
w_ref

# Expected asset return (4,65%)
exp.return.ref <- mu.row %*% w_ref
exp.return.ref

# Variance and standard deviation of asset return
var.returns.ref <- t(w_ref) %*% Sigma %*% w_ref
var.returns.ref

sd.returns.ref <- sqrt(var.returns.ref)
sd.returns.ref

######################### Optimal Portfolio of Risky Assets #################

# Required expected return 
r

# Weight nu(r)
nu_r <- (r - exp.return.min) / (exp.return.ref - exp.return.min)
nu_r

# Transform in a number (instead of a 1-cell matrix)
nu_r <- as.numeric(nu_r)
nu_r

# Optimal portfolio of risky assets with return requirement
w_r <- (1 - nu_r) * w_min + (nu_r * w_ref)
w_r

# Expected asset return
exp.return.r <- mu.row %*% w_r
exp.return.r

# Variance and standard deviation of asset return
var.returns.r <- t(w_r) %*% Sigma %*% w_r
var.returns.r

sd.returns.r <- sqrt(var.returns.r)
sd.returns.r

########################## Tangency Portfolio ###############################

# Additional assumption - Risk free rate of 1%
mu_0 <- 0.01

# Check how much this is
mu.col
mu_0 * ones.col
mu.col - mu_0 * ones.col
ones.row %*% Sigma.inv

# Break the formula down
(ones.row %*% Sigma.inv %*% (mu.col - mu_0 * ones.col))^(-1)
Sigma.inv %*% (mu.col - mu_0 * ones.col)

# Tangency portfolio
w_tan <- as.numeric((ones.row %*% Sigma.inv %*% (mu.col - mu_0 * ones.col))^(-1)) *
Sigma.inv %*% (mu.col - mu_0 * ones.col)
w_tan

w_tan

# Expected value, variance and standard deviation of asset return
t(w_tan) %*% mu.col 
var.returns.tan <- t(w_tan) %*% Sigma %*% w_tan
var.returns.tan

sd.returns.tan <- sqrt(var.returns.tan)
sd.returns.tan

##### Portfolio of risky assets with risk-free asset with return requirement #####

# Required expected return (for optimal portfolio of risky assets)
r

# The portfolio is w_0 * (risk-free asset) + (1 - w_0) * w_tan, 
# where 1 - w_0 = (r - mu_0) / (mu'w_tan - mu_0)

# Breaking down the formula
r - mu_0
mu.row %*% w_tan

# Portfolio of risky assets with risk-free asset with return requirement

# Weight of the tangency portfolio
weight.tan <- (r - mu_0) / as.numeric(mu.row %*% w_tan - mu_0)
weight.tan

# Weight of the risk free asset
w_0 <- 1 - weight.tan
w_0

# Variance and standard deviation of the overall return
var.returns.overall <- (1 - w_0) * var.returns.tan
var.returns.overall

sd.returns.overall <- sqrt(var.returns.overall)
sd.returns.overall


###########  Liability hedge portfolio ##################


sd.assets # standard deviation of assets
F_0 # Initial funding ratio

# Also from our assumptions: expected value, standard deviation and the correlations
# between our liability and our three assets.
mu.liab
sd.liab
rho

# First, we compute the covariances between our assets and our liability
gamma <- rho * sd.assets * sd.liab
gamma
gamma.col <- matrix(gamma, ncol = 1)


# Liability hedge portfolio

w_gamma <- (as.numeric(ones.row %*% Sigma.inv %*% gamma) ^ -1) * Sigma.inv %*% gamma
w_gamma

# Expected asset return (homework - 2.90%)


# Variance of asset return (homework - 0.0014)


# SD of asset return (homework - 3.75%)



######## Minimum variance portfolio to fund a stochastic liability #########

# weight of the liability hedge portfolio in the optimal portfolio - nu(F,gamma)
nu <- (1/F_0) * ones.row %*% Sigma.inv %*% gamma %>% as.numeric
nu

w_min_F_gamma <- (1 - nu) * w_min + nu * w_gamma
w_min_F_gamma

# Expected asset return
mu.row %*% w_min_F_gamma

# Expected surplus return
mu.surplus <- as.numeric(mu.row %*% w_min_F_gamma - mu.liab / F_0)
mu.surplus

# Variance of asset return
t(w_min_F_gamma) %*% Sigma %*% w_min_F_gamma


# Variance of surplus return
var.surplus <- t(w_min_F_gamma) %*% Sigma %*% w_min_F_gamma + 
  (sd.liab / F_0)^2 - 2*(t(w_min_F_gamma) %*% gamma.col) / F_0
var.surplus

# Standard deviation of asset return
sqrt(t(w_min_F_gamma) %*% Sigma %*% w_min_F_gamma)

# Standard deviation of surplus return
sqrt(var.surplus)

############### Optimal risky portfolio with liability hedge ######################

# Recall the required return rate, minimum variance portfolio and reference risky portfolio
r
w_ref
w_min

# Check parts of the formula for weight omega
mu.row %*% w_min_F_gamma
mu.row %*% w_ref

# Weight omega
omega <- (r - mu.row %*% w_min_F_gamma) / (mu.row %*% w_ref - mu.row %*% w_min)
omega <- as.numeric(omega)
omega

# Apply the formula
w_r_F_gamma <- w_min_F_gamma + omega * (w_ref - w_min)
w_r_F_gamma

# Homework: compute expected value, variance and SD of assets and surplus

######## Optimal risky portfolio with risk-free asset and liability hedge #########

# Recall the risk-free rate
mu_0

# Check parts of the formula for weight 1-w_0
nu * mu.row %*% (w_gamma - w_tan) 
r - nu * mu.row %*% (w_gamma - w_tan) - mu_0

# Weight 1 - w_0
weight <- (r - nu * mu.row %*% (w_gamma - w_tan) - mu_0) / (mu.row %*% w_tan - mu_0)
weight <- as.numeric(weight)
weight

# Weight of the risk-free asset
w_0_hedge <- 1 - weight
w_0_hedge

# Weights of the risky assets
w_risky_hedge <- (weight - nu) * w_tan  + nu * w_gamma
w_risky_hedge

# Homework: compute expected value, variance, SD of assets and surplus. Verify that:
#  - the SD of assets is larger than the SD of asset return of w_r, the optimal risk portfolio without hedge
#  - the SD of surplus is smaller than the SD of surplus return of w_r.